import { _decorator, Component, Node, UIOpacity, tween, Label, v3, Tween } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { SDKSystem, PlatformType } from '../../Init/SystemSDK/SDKSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
const { ccclass, property } = _decorator;

@ccclass('HomeUI')
export class HomeUI extends BasicUI {
    @property(Node)
    protected panel: Node = null;
    @property(Node)
    protected touchMask: Node = null;

    @property(Node)
    private startBtn: Node = null;

    protected onEvents() {
        this.on(EventTypes.GameEvents.EnterChooseLv, this.onEnterChooseLv, this);
    }

    public show(d) {
        super.show(d);

        this.touchMask.active = false;
        this.panel.active = true;
        this.startBtn.active = true;

        UISystem.showUI(UIEnum.PlayerAssetsUI);
        // this.emit(EventTypes.GameEvents.SetInitUIEnable, false);

        this.setLvNum();

        //自动进入
        if (GlobalTmpData.UIData.isEnterLv) {
            this.showLvScene();
        } else {
            AudioSystem.playBGM(AudioEnum.homeBgm);
        }
    }

    public hide(d?: any): void {
        this.node.active = false;
    }

    //进入游戏
    public enterGame() {
        //检测体力
        if (StorageSystem.getData().userAssets.asset < 1) {
            this.emit(EventTypes.GameEvents.ShowTips, '体力不够了!');
            UISystem.showUI(UIEnum.FreeGoldUI);
            return;
        }

        this.panel.active = false;
        this.startBtn.active = false;
        this.touchMask.active = true;
        //扣除体力动画
        this.emit(EventTypes.UIEvents.ShowCutEnergyAnim, () => {
            StorageSystem.setData((d) => {
                d.userAssets.asset--;
            }, true);
            StorageSystem.updateToAssets(false, false);
            //
            this.showLvScene();
        })
    }
    //显示场景
    public showLvScene(cb?) {
        this.emit(EventTypes.GameEvents.GameStart, () => {
            console.log("## GameLoadFinish ")
            this.emit(EventTypes.GameEvents.GameLoadFinish);
            cb && cb();
        });
    }

    // #region -----------------私有-----------
    @property(Label)
    protected lvLabel: Label = null;
    /**关卡序号 */
    private setLvNum() {
        if (this.lvLabel) {
            let lv = StorageSystem.getData().levelAssets.curLv;
            this.lvLabel.string = (lv).toFixed(0);
        }
    }

    // #endregion

    // #region -----------------按钮--------------
    /**点击开始按钮 */
    public onGameStartClick() {
        //默认从最大关卡开始
        StorageSystem.setData((d) => {
            d.levelAssets.curLv = d.levelAssets.maxLv;
        }, true)

        this.enterGame();
    }

    /**显示设置UI */
    protected onShowSettingUI() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        UISystem.showUI(UIEnum.SettingUI);
    }

    //隐私UI
    protected onShowPrivacyUI() {
        UISystem.showUI(UIEnum.PrivacyUI, { isLobby: true });
    }

    // #endregion

    // #region -----------------事件------------

    //进入指定关卡
    protected onEnterChooseLv(lv: number) {
        StorageSystem.setData((d) => {
            d.levelAssets.curLv = lv;
        });
        this.enterGame();
    }


    // #endregion

}

